<?php
$con=mysqli_connect("localhost","root","youhanmo1997");
if(!$con){
	die("error:".mysqli_errno()) ;
}  

mysqli_select_db($con,"students_management");	
$sql= "SELECT * FROM students S,courses C,course_select CS Where S.sid=CS.sid AND CS.cid=C.cid ";
foreach($_POST as $key => $value){
	if(($value!="")&&($key=="sid"||$key=="classnum")){
		$sql.="AND S.".$key."='".$value."'";
	}else if(($value!="")&&($key=="cid"||$key=="cname")){
		$sql.="AND C.".$key."='".$value."'";
	}
}

$sql.=";";


// $result = mysqli_query($con,$sql);
// if (!$result) {
//  printf("Error: %s\n", mysqli_error($con));
//  exit();
// }
// $arr = array();
// while($row=mysqli_fetch_array($result)){
// 	$arr[] = $row;
// }

$score_list= "SELECT count(*) as total FROM students S,courses C,course_select CS Where S.sid=CS.sid AND CS.cid=C.cid ";
foreach($_POST as $key => $value){
	if(($value!="")&&($key=="sid"||$key=="classnum")){
		$score_list.="AND S.".$key."='".$value."'";
	}else if(($value!="")&&($key=="cid"||$key=="cname")){
		$score_list.="AND C.".$key."='".$value."'";
	}
}

$score_list_0_60=$score_list." AND ((CS.score>=0) AND (CS.score<60)) ;";
$score_list_60_70=$score_list." AND ((CS.score>=60) AND (CS.score<70)) ;";
$score_list_70_80=$score_list." AND ((CS.score>=70) AND (CS.score<80)) ;";
$score_list_80_90=$score_list." AND ((CS.score>=80) AND (CS.score<90)) ;";
$score_list_90_100=$score_list." AND ((CS.score>=90) AND (CS.score<100)) ;";
$score_list_100=$score_list." AND (CS.score=100) ;";

$arr = array();
//0-60
$result_0_60=mysqli_query($con,$score_list_0_60);
if (!$result_0_60) {
 printf("Error: %s\n", mysqli_error($con));
 exit();
}
while($row=mysqli_fetch_array($result_0_60)){
	$row['name'] = "不及格";
	$arr[] = $row;
}
//60-70
$result_60_70=mysqli_query($con,$score_list_60_70);
if (!$result_60_70) {
 printf("Error: %s\n", mysqli_error($con));
 exit();
}
while($row=mysqli_fetch_array($result_60_70)){
	$row['name'] = "60-69";
	$arr[] = $row;
}
//70-80
$result_70_80=mysqli_query($con,$score_list_70_80);
if (!$result_70_80) {
 printf("Error: %s\n", mysqli_error($con));
 exit();
}
while($row=mysqli_fetch_array($result_70_80)){
	$row['name'] = "70-79";
	$arr[] = $row;
}
//80-90
$result_80_90=mysqli_query($con,$score_list_80_90);
if (!$result_80_90) {
 printf("Error: %s\n", mysqli_error($con));
 exit();
}
while($row=mysqli_fetch_array($result_80_90)){
	$row['name'] = "80-89";
	$arr[] = $row;
}
//90-100
$result_90_100=mysqli_query($con,$score_list_90_100);
if (!$result_90_100) {
 printf("Error: %s\n", mysqli_error($con));
 exit();
}
while($row=mysqli_fetch_array($result_90_100)){
	$row['name'] = "90-99";
	$arr[] = $row;
}
//100
$result_100 = mysqli_query($con,$score_list_100);
if (!$result_100) {
 printf("Error: %s\n", mysqli_error($con));
 exit();
}
// $arr = array();
while($row=mysqli_fetch_array($result_100)){
	$row['name'] = "满分";
	$arr[] = $row;
}




echo json_encode($arr);

///////////////

// $stu_arr=array();
//  $score_list = array();
//     foreach ($stu_arr as $value) {
//       $res = $D -> findAll($D -> query("select SC.sid,SC.cid,SC.score,S.name as sname,S.class,C.name as cname,C.teacher_name,C.credit from tju_select_course as SC left join tju_baseinfo_student as S on SC.sid = S.sid left join tju_baseinfo_course C on SC.cid = C.cid where SC.sid = '$value[sid]' and del_time is null"));
//       if($res){
//         foreach ($res as $e) {
//           $score_list[] = $e;
//         }
//       }
//     }
//     if(empty($score_list)){
//       echo json_encode(array(
//         'err' => 404,
//         'msg' => 'not found'
//       ));
//       return;
//     }
//   else{
//     echo json_encode(array(
//       'err' => 1,
//       'msg' => 'Incomplete parameter'
//     ));
//     return;
//   }
//   // var_dump($score_list);
//   $total_credit = 0;
//   $total_wei = 0;
//   $range_total = array(0,0,0,0,0,0,0,0,0,0,0);
//   foreach ($score_list as $value) {
//     if(empty($value['score'])){
//       continue;
//     }
//     $total_credit += $value['credit'];
//     $total_wei += $value['score'] * $value['credit'];
//     $range_total[floor($value['score']/10)]++;
//   }
//   if($total_credit == 0){
//     $ave_score = 0;
//   }else{
//     $ave_score = round($total_wei / $total_credit,2);
//   }
//   echo json_encode(array(
//     'err' => 0,
//     'type' => $type,
//     'scorelist' => $score_list,
//     'ave_score' => $ave_score,
//     'range_total' => $range_total,
//     'info' => $info
//   ));





?>